package com.example.toy.core.utils;

import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

/**
 * WaitUtils
 *
 * @author shenbing
 * @date 2021/12/22
 */
public class WaitUtils {

  public static void wait(long interval, long timeout, String msg, Supplier<Boolean> supplier)
      throws TimeoutException, InterruptedException {
    long sleepTime = 0l;
    while (!supplier.get()) {
      if (sleepTime >= timeout) {
        throw new TimeoutException("timeout to wait condition: " + msg);
      }
      Thread.sleep(interval);
      sleepTime += interval;
    }
  }
}
